package com.amin.app.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.amin.app.web.entity.*;
import com.amin.app.web.service.MenuSerivce;
import com.amin.app.web.service.RoleMenuService;
import com.amin.app.web.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSessionException;
import org.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 系统角色控制器
 */
@Controller
public class RoleController {
    final
    RoleService roleService;
    final
    MenuSerivce menuSerivce;
    final
    RoleMenuService roleMenuService;

    public RoleController(RoleService roleService, MenuSerivce menuSerivce, RoleMenuService roleMenuService) {
        this.roleService = roleService;
        this.menuSerivce = menuSerivce;
        this.roleMenuService = roleMenuService;
    }

    /**
     * 获取角色菜单
     * @param model
     * @param page
     * @param sRole
     * @return
     */
    @RequestMapping(value = "/role/getRoleList")
    public String getRoleList(Model model, Page page, SRole sRole){
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SRole> roleList = roleService.getRoleList(sRole);
        PageInfo<SRole> roleListPage = new PageInfo<>(roleList);
        model.addAttribute("roleList",roleListPage);
        model.addAttribute("sRole",sRole);
        return "/amin/role/roleList";
    }
    /**
     * 新增编辑跳转
     * @param model
     * @param sRole
     * @return
     */
    @RequestMapping(value = "/role/createRole")
    public String createRole(Model model, SRole sRole){
        if(null != sRole.getId() && 0 != sRole.getId()){
            sRole = roleService.getRoleById(sRole.getId());
        }
        model.addAttribute("role",sRole);
        return "/amin/role/createRole";
    }
    /**
     * 配置菜单
     * @param model
     * @param sRole
     * @return
     */
    @RequestMapping(value = "/role/roleMenu")
    public String roleMenu(Model model, SRole sRole) throws JSONException {
        SMenu sMenu = new SMenu();
        List<SMenu> menuList= menuSerivce.getMenuList(sMenu);
        List<RoleMenu> menuListJson = new ArrayList<>();
        org.json.JSONArray menuListjson = new org.json.JSONArray();
        for (SMenu menu:menuList){
            org.json.JSONObject jo = new org.json.JSONObject();
            jo.put("value", menu.getId());
            jo.put("title",menu.getMenuName());
            menuListjson.put(jo);
        }
        List<RoleMenu> roleMenuList = roleMenuService.getMenuListByRoleId(sRole.getId());
        List<Integer> roleMenuJson = new ArrayList<>();
        for (RoleMenu roleRenu:roleMenuList){
            roleMenuJson.add(roleRenu.getMenuId());
        }
        model.addAttribute("menuList", menuListjson);
        model.addAttribute("roleMenuList", roleMenuJson);
        model.addAttribute("role", sRole);
        return "/amin/role/roleMenu";
    }

    /**
     * 保存菜单配置数据
     * @param model
     * @param sRole
     * @param menuList
     * @return
     * @throws JSONException
     */
    @ResponseBody
    @RequestMapping(value = "/role/roleMenuSave")
    public Map<String, Object> roleMenuSave(Model model, SRole sRole, String menuList) throws JSONException {
        Map<String,Object> res = new HashMap<>();
        List<RoleMenuJson> roleMenuJson = JSONArray.parseArray(menuList,RoleMenuJson.class);
        roleMenuService.delById(sRole.getId());
        for (RoleMenuJson menu: roleMenuJson){
            roleMenuService.save(menu.getValue(),sRole.getId(),new Date());
        }
        res.put("code",0);
        return res;
    }


    /**
     * 数据提交
     * @param sRole
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/role/dataSubmit")
    public Map<Object,Object> dataSubmit(SRole sRole){
        Map<Object,Object> answer = new HashMap<>();
        try {
            if (null == sRole.getId() || 0 ==  sRole.getId()){
                sRole.setCreatedTime(new Date());
                roleService.save(sRole);
            }else {
                roleService.update(sRole);
            }
            answer.put("code",0);
            answer.put("msg","数据保存成功");
        }catch (SqlSessionException e){
            answer.put("code",1);
            answer.put("msg",e.getMessage());
        }
        return answer;
    }

    /**
     * 删除角色
     * @param model
     * @param sRole
     * @return
     */
    @RequestMapping(value = "/role/delById")
    public String delById(Model model, SRole sRole){
        roleService.delById(sRole.getId());
        return "redirect:/role/getRoleList";
    }
}
